﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace DependencyAnalysis.DataModel
{
    [Serializable]
    [DataContract(IsReference = true)]
    [KnownType(typeof(SubmoduleDescriptor))]
    public class ModuleDescriptor : IDescriptor
    {
        private string _identifier;

        [DataMember(Order = 1)]
        public string Identifier
        {
            get { return _identifier; }
            set
            {
                _identifier = value;
                ComparableIdentifier = ("" + _identifier).ToLowerInvariant();
            }
        }

        [DataMember(Order = 2)]
        public string FullName { get; set; }
        [DataMember(Order = 3)]
        public bool IsPartOfProject { get; set; }
        [DataMember(Order = 4)]
        public bool IsPartOfFramework { get; set; }
        public IDescriptor Parent { get { return null; } }
        public string Name { get { return FullName; } }

        [DataMember(Order = 5)]
        private IList<SubmoduleDescriptor> _submodules = new List<SubmoduleDescriptor>();
        public IList<SubmoduleDescriptor> Submodules
        {
            get { return _submodules; }
            set { _submodules = value; }
        }

        public override bool Equals(object obj)
        {
            if (!(obj is ModuleDescriptor)) return false;

            return String.Equals(ComparableIdentifier, ((ModuleDescriptor)obj).ComparableIdentifier);
        }

        internal string ComparableIdentifier { get; set; }

        public override int GetHashCode()
        {
            return ("" + ComparableIdentifier).GetHashCode();
        }

        public override string ToString()
        {
            return GetType().Name + ": " + FullName;
        }
    }
}
